/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.library.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.base.ApiResult;
import com.library.entity.RecommendManageEntity;
import com.library.service.RecommendManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 推荐位管理表
 *
 * @author pig
 * @date 2023-08-03 11:42:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/recommendManage")
@Api(tags = "推荐位管理表管理", value = "推荐位管理表管理")
public class RecommendManageController {

    @Resource
    private RecommendManageService recommendManageService;


    /**
     * 分页查询
     *
     * @param page              分页对象
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ApiResult getrecommendManagePage(Page page, RecommendManageEntity recommendManage) {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(recommendManage.getResourceName()), RecommendManageEntity::getResourceName, recommendManage.getResourceName());
        wrapper.like(StrUtil.isNotBlank(recommendManage.getAuthor()), RecommendManageEntity::getAuthor, recommendManage.getAuthor());
        return ApiResult.ok(recommendManageService.page(page, wrapper));
    }


    /**
     * 通过id查询推荐位管理表
     *
     * @param id id
     * @return ApiResult
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("/{id}")
    public ApiResult getById(@PathVariable("id") Long id) {
        return ApiResult.ok(recommendManageService.getById(id));
    }

    /**
     * 新增推荐位管理表
     *
     * @param recommendManage 推荐位管理表
     * @return ApiResult
     */
    @ApiOperation(value = "新增推荐位管理表")
    @PostMapping("add")
    public ApiResult save(@RequestBody RecommendManageEntity recommendManage) {

        return ApiResult.ok(recommendManageService.save(recommendManage));
    }

    /**
     * 修改推荐位管理表
     *
     * @param recommendManage 推荐位管理表
     * @return ApiResult
     */
    @ApiOperation(value = "修改推荐位管理表")
    @PostMapping("update")
    public ApiResult updateById(@RequestBody RecommendManageEntity recommendManage) {
        return ApiResult.ok(recommendManageService.updateById(recommendManage));
    }

    /**
     * 通过id删除推荐位管理表
     *
     * @param ids id列表
     * @return ApiResult
     */
    @ApiOperation(value = "通过id删除推荐位管理表")
    @PostMapping("delete")
    public ApiResult removeById(@RequestBody Long[] ids) {
        return ApiResult.ok(recommendManageService.removeByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param recommendManage 查询条件
     * @return excel 文件流
     */
    @GetMapping("/export")
    public List<RecommendManageEntity> export(RecommendManageEntity recommendManage) {
        return recommendManageService.list(Wrappers.query(recommendManage));
    }

    @GetMapping("/get/getByResources/{bookName}")
    public ApiResult getByResources(@PathVariable("bookName") String bookName) {
        return recommendManageService.getByResources(bookName);
    }

    @GetMapping("/get/getList/{dataTypeId}")
    public ApiResult getList(@PathVariable("dataTypeId") Long dataTypeId) {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RecommendManageEntity::getDataTypeId,dataTypeId);
        wrapper.eq(RecommendManageEntity::getState,"1");
        return ApiResult.ok(recommendManageService.list(wrapper));
    }
//
//    @GetMapping("/timeSequence")
//    public ApiResult timeSequence() {
//        ApiResult.ok("");
//    }
}
